package com.jumper.study.admin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.admin.dto.req.ApiReq;
import com.jumper.study.admin.dto.resp.ApiResp;
import com.jumper.study.admin.service.ApiService;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.api.ResultCode;
import com.jumper.study.db.model.Api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;




@RestController
@RequestMapping("/api/backend/apis")
public class ApiController {
    @Autowired
    private ApiService apiService;

    @GetMapping("")
    public CommonResult<List<ApiResp>> getList() {
        List<ApiResp> apiList = apiService.list().stream()
                    .map(m -> new ApiResp(m.getId(), m.getTitle(), m.getUri(), m.getMethod(), m.getDesc(), m.getGroup(), m.getCreatedAt()))
                    .toList();

        return CommonResult.success(apiList);
    }
    
    @GetMapping("/{id}")
    public CommonResult<ApiResp> getItem(@PathVariable Integer id) {
        Api api = apiService.getById(id);
        if (api == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        ApiResp resp = new ApiResp(api.getId(), api.getTitle(), api.getUri(), api.getMethod(), api.getDesc(), api.getGroup(), api.getCreatedAt());

        return CommonResult.success(resp);
    }
    
    @PostMapping("/")
    public CommonResult<Boolean> create(@RequestBody @Validated ApiReq req) {
        Api api = new Api();
        api.setTitle(req.getTitle());
        api.setUri(req.getUri());
        api.setMethod(req.getMethod());
        api.setDesc(req.getDesc());
        api.setGroup(req.getGroup());

        boolean result = apiService.save(api);
        return CommonResult.success(result);
    }

    @PostMapping("/{id}")
    public CommonResult<Boolean> update(@PathVariable Integer id, @RequestBody @Validated ApiReq req) {
        Api api = apiService.getById(id);
        if (api == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        api.setTitle(req.getTitle());
        api.setUri(req.getUri());
        api.setMethod(req.getMethod());
        api.setDesc(req.getDesc());
        api.setGroup(req.getGroup());
        boolean result = apiService.updateById(api);

        return CommonResult.success(result);
    }
    
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = apiService.removeById(id);

        return CommonResult.success(result);
    }
    
}
